//
//  ZTAnchorViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTAnchorViewController.h"
#import "AppTools.h"
#import "ZHListBigModel.h"
#import "ZHListModel.h"
#import "ZHAnchorDetailViewController.h"
#import "MJRefresh.h"
#import "ZHAnchorMoreViewController.h"
#import "ZHAnchorCollectionViewCell.h"
#import "ZHCollectionReusableView.h"

@interface ZTAnchorViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource,ZHCollectionReusableViewDelegate>
// 存model数据
@property (nonatomic,strong)NSMutableArray *bigArray;


@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,assign)NSInteger currentPage;


@end

@implementation ZTAnchorViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.messageView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    self.bigArray = [NSMutableArray array];
//    self.listArray = [NSMutableArray array];
    
    [self getData];
    
   
    // 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH_ZH - 40)/3, 140);
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH_ZH, 40);
//    flowLayout.minimumInteritemSpacing = 5;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH - 44) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ZHAnchorCollectionViewCell class] forCellWithReuseIdentifier:@"ZHAnchorCollectionViewCell"];
    [_collectionView registerClass:[ZHCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZHCollectionReusableView"];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    
    
    self.currentPage = 1;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _collectionView.mj_footer = footer;

    
}
#pragma mark - 请求数据
- (void)getData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=iphone&page=%ld",self.currentPage];
//    NSLog(@"ssss -- %@",urlStr);
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        NSMutableArray *bigListArray = [result objectForKey:@"list"];

        for (NSDictionary *dic in bigListArray) {

            ZHListBigModel *bigModel = [[ZHListBigModel alloc]initWithDictionary:dic];
//            NSLog(@"=== %@",bigModel.listArray);
             NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dicM in bigModel.listArray) {
                ZHListModel *listModel = [[ZHListModel alloc]initWithDictionary:dicM];
                [array addObject:listModel];
            }
            bigModel.listArray = [NSMutableArray arrayWithArray:array];
            [array removeAllObjects];
            [_bigArray addObject:bigModel];

        }
        self.currentPage++;
        [_collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        
    }];


}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    NSLog(@"ssss -- %ld",self.bigArray.count);
    return self.bigArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
   ZHListBigModel *bigModel = [self.bigArray objectAtIndex:section];
    NSLog(@"ssss -- %ld",bigModel.listArray.count);
    return bigModel.listArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"ZHAnchorCollectionViewCell";
    ZHAnchorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    ZHListBigModel *bigModel = [self.bigArray objectAtIndex:indexPath.section];
    ZHListModel *model = [bigModel.listArray objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}
// 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    ZHCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ZHCollectionReusableView" forIndexPath:indexPath];
    ZHListBigModel *model = [self.bigArray objectAtIndex:indexPath.section];
    reusableView.bigModel = model;
    reusableView.anchorCollectionDelegate = self;
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ZHAnchorDetailViewController *anchorDetailVC = [[ZHAnchorDetailViewController alloc]init];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc]initWithRootViewController:anchorDetailVC];
    [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:anchorDetailVC];

    ZHListBigModel *bigModel = [self.bigArray objectAtIndex:indexPath.section];
    ZHListModel *model = [bigModel.listArray objectAtIndex:indexPath.item];
    anchorDetailVC.uid = [model.uid integerValue];
    anchorDetailVC.titleName = model.nickname;
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
        
    }];

}
- (void)presentToMoreViewController:(NSInteger)btnTag name:(NSString *)labelName{
    ZHAnchorMoreViewController *moreVC = [[ZHAnchorMoreViewController alloc]init];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc]initWithRootViewController:moreVC];
    [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:moreVC];
    
    moreVC.titleName = labelName;
    
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
